<template>
  <div class="server-main">
    <el-form :inline="true" class="search-form">
      <el-form-item label="抽取模型：" class="form-input">
        <el-select clearable v-model="filterData.model_id" placeholder="请选择">
          <el-option
            v-for="item in extractModelList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="searchKA"
          >查询</el-button
        >
      </el-form-item>
    </el-form>
    <div class="file-table">
      <div class="table-title">
        <span class="table-title-text">知识获取记录</span>
        <div class="table-title-button">
          <el-button type="primary" size="small" @click="goAcquireKnowledge()"
            >知识获取</el-button
          >
        </div>
      </div>
      <el-table :data="tableList" class="km-table" v-loading="tableLoading">
        <el-table-column label="序号" align="center" type="index" width="50">
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="抽取时间"
          align="center"
          min-width="150"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="modelName"
          label="抽取模型"
          align="center"
          width="150"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="entityNum"
          label="抽取实体数量"
          align="center"
          min-width="100"
        ></el-table-column>
        <el-table-column
          prop="relationNum"
          label="抽取关系数量"
          min-width="100"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="propertyNum"
          label="抽取属性数量"
          min-width="100"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-link
              :underline="false"
              type="primary"
              class="form-action"
              @click.native.prevent="goDetail(scope.row.id)"
              >查看</el-link
            >
            <el-link
              :underline="false"
              type="primary"
              class="form-action"
              @click.native.prevent="onDelete(scope.row.id)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="km-pagination"
        background
        :small="true"
        layout="total, prev, pager, next,sizes, jumper"
        :total="total"
        :hide-on-single-page="!total"
        :page-size.sync="filterData.page_size"
        :pager-count="7"
        :page-sizes="[10, 20, 30]"
        :current-page="filterData.page_index"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  name: "knowledgeGet",
  components: {},
  data: () => {
    return {
      filterData: {
        model_id: "", // 文件名称
        page_index: 1, // 页码
        page_size: 10, // 每页条数
      },
      tableList: [], // 文件列表
      // 表格中选中的
      tableSelected: [],
      extractModelList: [], // 抽取模型列表
      tableLoading: false,
      total: 0, // 数据总条数
    };
  },
  computed: {},
  created() {
    // 知识获取记录列表
    this.getKAList(this.filterData);

    // 获取抽取模型列表
    this.getExtractModel();
  },
  methods: {
    searchKA() {
      this.filterData.page_index = 1;
      this.getKAList(this.filterData);
    },
    // 知识获取记录
    getKAList(params) {
      this.tableLoading = true;
      this.$api.knowledgeGeneration
        .getKAList(params)
        .then((res) => {
          console.log(res);
          const data = res.data.data;
          this.total = data.total;
          this.tableList = data.list;
        })
        .finally(() => (this.tableLoading = false));
    },

    getExtractModel() {
      this.$api.knowledgeGeneration.getKAModel().then((res) => {
        const { code, data, message } = res.data;
        if (code !== 200) {
          console.error(res);
          this.$message.error(message);
          return;
        }
        this.extractModelList = data.map((extractMode) => ({
          id: extractMode.id,
          title: extractMode.modelName,
        }));
      });
    },

    // 跳转至获取知识页面
    goAcquireKnowledge() {
      this.$router.push("/knowledgeGeneration/knowledgeGet/acquireKnowledge");
    },
    // 修改当前页码
    handleCurrentChange(page) {
      this.filterData.page_index = page;
      this.getKAList(this.filterData);
    },
    // 修改每页条数
    handleSizeChange() {
      this.getKAList(this.filterData);
    },

    // 查看
    goDetail(id) {
      this.$router.push({
        path: "/knowledgeGeneration/knowledgeGet/detail",
        query: {
          id,
        },
      });
    },
    // 删除点击事件
    onDelete(id) {
      console.log("id", id);
      this.$confirm(
        "是否确认删除所选知识获取记录？删除后数据不可恢复，请谨慎操作。",
        "删除知识获取记录",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(async () => {
          const res = await this.$api.knowledgeGeneration.deleteKA({
            recordId: id,
          });
          if (res.data.code == 200) {
            this.$message.success(res.data.message);
            this.getKAList(this.filterData);
          } else {
            this.$message.error(res.data.message);
          }
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
.search-form {
  background: #ffffff;
  border-radius: 4px;
  padding: 16px 32px;
  .el-form-item {
    margin-bottom: 0;
  }

  .form-input {
    margin-right: 32px;
    .el-select {
      ::v-deep .el-input__inner {
        width: 240px;
        height: 32px;
        background: #ffffff;
        border-radius: 2px;
        border: 1px solid #e0e0e0;
      }
    }
  }
}

.file-table {
  margin-top: 16px;
  padding: 16px 32px 64px;
  background: #ffffff;
  border-radius: 4px;

  .table-title {
    display: flex;
    justify-content: space-between;
    align-items: center;

    &-text {
      margin: 8px 0;
      font-size: 16px;
      line-height: 16px;
      border-left: 5px solid $--color-primary;
      padding-left: 10px;
    }

    &-button {
      display: flex;

      .el-button {
        margin-right: 20px;
      }
    }

    margin-bottom: 10px;
  }
}

.server-main {
  .form-action {
    padding: 0 10px;
    & + .form-action {
      border-left: 1px solid #dddddd;
    }
  }

  ::v-deep .el-dialog {
    width: 500px;
    border-radius: 5px;
  }

  .dialog-inline {
    ::v-deep .el-form-item__content {
      width: 300px;
    }

    button.el-button {
      background-color: $--color-primary !important;
      color: #fff !important;
      line-height: 1.2;
    }
  }
}
</style>
